package com.alibaba.middleware.race.rpc.netty;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

public class PoolableRpcConnectionFactory implements RpcConnectionFactory,PoolableObjectFactory<RpcConnection> {

	private RpcConnectionFactory connectionFactory;
	private GenericObjectPool<RpcConnection> pool = new GenericObjectPool<RpcConnection>(this);
	
	public PoolableRpcConnectionFactory(RpcConnectionFactory factory)
	{
		if(factory==null)
		{
			throw new NullPointerException("factory");
		}
		this.connectionFactory=factory;
	}
	@Override
	public RpcConnection getConnection() throws Throwable {
		// TODO Auto-generated method stub
		return pool.borrowObject();
	}

	@Override
	public void recycle(RpcConnection connection) throws Throwable {
		// TODO Auto-generated method stub
		if (null != connection) {
			pool.returnObject(connection);
		}
	}
	public void destroy() throws Throwable {
		pool.close();
	}
	@Override
	public void activateObject(RpcConnection connection) throws Exception {
		// TODO Auto-generated method stub
		try {
			connection.connection();
		} catch (Throwable e) {
			throw new Exception(e);
		}
	}

	@Override
	public void destroyObject(RpcConnection connection) throws Exception {
		// TODO Auto-generated method stub
		try {
			connection.close();
		} catch (Throwable e) {
			throw new Exception(e);
		}
	}

	@Override
	public RpcConnection makeObject() throws Exception {
		// TODO Auto-generated method stub
		try {
			return connectionFactory.getConnection();
		} catch (Throwable e) {
			throw new Exception(e);
		}
	}

	@Override
	public void passivateObject(RpcConnection arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean validateObject(RpcConnection connection) {
		// TODO Auto-generated method stub
		return connection.isConnected() && !connection.isClosed();
	}
	public void setLifo(boolean lifo) {
		pool.setLifo(lifo);
	}

	public void setMaxActive(int maxActive) {
		pool.setMaxActive(maxActive);
	}

	public void setMaxIdle(int maxIdle) {
		pool.setMaxIdle(maxIdle);
	}

	public void setMaxWait(long maxWait) {
		pool.setMaxWait(maxWait);
	}

	public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
		pool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	}

	public void setMinIdle(int minIdle) {
		pool.setMinIdle(minIdle);
	}

	public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
		pool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
	}

	public void setSoftMinEvictableIdleTimeMillis(
			long softMinEvictableIdleTimeMillis) {
		pool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
	}

	public void setTestOnBorrow(boolean testOnBorrow) {
		pool.setTestOnBorrow(testOnBorrow);
	}

	public void setTestOnReturn(boolean testOnReturn) {
		pool.setTestOnReturn(testOnReturn);
	}

	public void setTestWhileIdle(boolean testWhileIdle) {
		pool.setTestWhileIdle(testWhileIdle);
	}

	public void setTimeBetweenEvictionRunsMillis(
			long timeBetweenEvictionRunsMillis) {
		pool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
	}

	public void setWhenExhaustedAction(byte whenExhaustedAction) {
		pool.setWhenExhaustedAction(whenExhaustedAction);
	}
}
